<?php

namespace app\controller;

//玩家探索控制器
use app\service\UserExploreService;
use app\service\UserFactoryService;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

class UserExplore extends Base
{


    //获取探索星球列表
    public function get_explore_address_list(): Response
    {
        try {
            $res = (new UserExploreService())->get_explore_address_list($this->user_id);
            return $this->json($res);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }
    }

    //获取探索章节列表
    public function get_explore_chapter_list(): Response
    {
        $explore_address_id = $this->request->get('explore_address_id');
        try {
            $res = (new UserExploreService())->get_explore_chapter_list($this->user_id,$explore_address_id);
            return $this->json($res);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }
    }

    //获取探索故事列表
    public function get_explore_story_list(): Response
    {
        $explore_chapter_id = $this->request->get('explore_chapter_id');
        try {
            $res = (new UserExploreService())->get_explore_story_list($this->user_id,$explore_chapter_id);
            return $this->json($res);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }
    }


    //获取探索任务列表
    public function get_explore_task_list(): Response
    {
        $explore_story_id = $this->request->get('explore_story_id');
        try {
            $res = (new UserExploreService())->get_explore_task_list($this->user_id,$explore_story_id);
            return $this->json($res);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }
    }


    //获取探索导航-收录任务
    public function get_explore_navigation(): Response
    {
        $explore_address_id = $this->request->get('explore_address_id');
        try {
            $res = (new UserExploreService())->get_explore_navigation_list($this->user_id,$explore_address_id);
            return $this->json($res);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }


    }

    //get_user_explore_reward_list 获取玩家探索奖励列表
    public function get_user_explore_reward_list(): Response
    {
        $explore_address_id = $this->request->get('explore_address_id');
        try {
            $res = (new UserExploreService())->get_user_explore_reward_list($this->user_id,$explore_address_id);
            return $this->json($res);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            return $this->fail($e->getMessage());
        }
    }


    //receive_explore_reward 领取探索奖励
    public function receive_explore_reward(): Response
    {
        $user_explore_task_id = $this->request->post('user_explore_task_id');
        Db::startTrans();
        try {
            $res = (new UserExploreService())->receive_explore_reward($this->user_id,$user_explore_task_id);
            if ($res['code']) {
                Db::commit();
            } else {
                Db::rollback();
            }
            return $this->json($res);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            Db::rollback();
            return $this->fail($e->getMessage());
        }
    }




    //提交工具
    public function submit_tool(): Response
    {
        $explore_task_id = $this->request->post('explore_task_id');
        Db::startTrans();
        try {
            $res = (new UserExploreService())->submit_tool($this->user_id, $explore_task_id);
            if ($res['code']) {
                Db::commit();
            }else{
                Db::rollback();
            }
            return $this->json($res);
        } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
            Db::rollback();
            return $this->fail($e->getMessage());
        }


    }


}